package com.atm.controller;

import com.atm.annotation.NeedLogin;
import com.atm.config.Const;
import com.atm.service.AccountService;
import com.atm.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "账户")
@RestController
public class AccountController {
    @Resource
    private AccountService accountService;

    @ApiOperation("修改密码")
    @NeedLogin
    @PutMapping("/changePassword")
    public Result<Boolean> modifyPassword(String oldPassword, String newPassword, String repeatPassword, HttpServletRequest req) {
        return accountService.modifyPassword(oldPassword, newPassword, repeatPassword, req.getAttribute(Const.AccountNo).toString());
    }

    @ApiOperation("转账")
    @NeedLogin
    @PutMapping("/transfer")
    public Result<Boolean> transfer(String toAccountId, Long transferAmount, HttpServletRequest req) {
        return accountService.transfer(toAccountId, transferAmount, req.getAttribute(Const.AccountNo).toString());
    }

    @ApiOperation("查询用户名")
    @NeedLogin
    @PostMapping("/username")
    public Result<String> searchUsername(HttpServletRequest req) {
        return accountService.selectUsernameByAccountNo(req.getAttribute(Const.AccountNo).toString());
    }

    @ApiOperation("检查token")
    @NeedLogin
    @PostMapping("/checkToken")
    public void checkToken() {}
}
